/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：Draw2dContext.js<br>
 * 日期：2016年9月23日<br>
 * 作者: 王志远<br>
 * 功能：Draw2d上下文环境<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");


/**
 * 类型名称：Rainier.UI.V2.AbstractEditPart<br>
 * 类型功能：抽象编辑件<br>
 */
Rainier.UI.V2.Draw2dContext = Rainier.Basic.CompositeObject.extend({
	
	NAME : "Rainier.UI.V2.Draw2dContext",
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dContext.init<br>
	 * 函数功能：构造函数<br>
	 * 异常信息：无<br>
	 */
	init : function(){
		this._super();
		this.rootEditPart = null;
		this.rootModel    = null;
		this.rootFigure   = null;
		this.experimentCfg = null;
		
		//选中对象列表
		this.selected         = new draw2d.util.ArrayList();
		//拷贝对象列表
		this.copyModelList    = new draw2d.util.ArrayList();
		//标签列表
		this.labelList     	  = {};
		//电路解算器
		this.circuitparser    = null;
		//菜单项使用控制信息
		this.menuItemUsedInfo = {
				deviceInfoUsed	: true,
				deviceShowAll   : false,
				guideLineUsed	: false,
				jointPortUsed	: true,
				copyUsed		: false,
				canShowAllImg	: false,
				canHideAllImg	: false,
		};
		
		this.lineNumberGenerator   = new Rainier.UI.V2.LineNumberGenerator();	//连线节点生成器;
		this.canvasContactPortList = new draw2d.util.ArrayList();				//canvas中 存在关联的port 的数组
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dContext.getSelected<br>
	 * 函数功能：返回选中的model<br>
	 * 函数返回：draw2d.util.ArrayList()<br>
	 * 异常信息：无<br>
	 */
	getSelected:function(){
		return this.selected;	
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dContext.setCircuitParser<br>
	 * 函数功能：通过rootModel设置解算器，必须先调用setRootModel<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setCircuitParser : function(){
		this.circuitparser = new Rainier.UI.V2.CircuitParser(this.rootModel);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dContext.getCircuitParser<br>
	 * 函数功能：getCircuitParser 获取解算器<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：circuitparser ：Rainier.UI.V2.CircuitParser <br>
	 * 异常信息：无<br>
	 */
	getCircuitParser : function(){
		return this.circuitparser;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dContext.setRootEditPart<br>
	 * 函数功能：设置根编辑件<br>
	 * 参数输入：rootEditPart:Rainier.UI.V2.RootEditPart<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setRootEditPart : function(rootEditPart){
		this.rootEditPart = rootEditPart;
		this.rootEditPart.context = this;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dContext.setRootModel<br>
	 * 函数功能：设置根模型<br>
	 * 参数输入：rootModel:Rainier.UI.V2.RootModel<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setRootModel : function(rootModel){
		this.rootModel = rootModel;
		this.rootModel.context = this;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dContext.setRootFigure<br>
	 * 函数功能：设置根图元<br>
	 * 参数输入：rootFigure:Rainier.UI.V2.Canvas<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setRootFigure : function(rootFigure){
		this.rootFigure = rootFigure;
		this.rootFigure.context = this;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dContext.getRootEditPart<br>
	 * 函数功能：获取根编辑件<br>
	 * 函数返回：Rainier.UI.V2.RootEditPart<br>
	 * 异常信息：无<br>
	 */
	getRootEditPart : function(){
		return this.rootEditPart;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dContext.getRootModel<br>
	 * 函数功能：获取根模型<br>
	 * 函数返回：Rainier.UI.V2.RootModel<br>
	 * 异常信息：无<br>
	 */
	getRootModel : function(){
		return this.rootModel;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dContext.getRootFigure<br>
	 * 函数功能：获取根图元<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	getRootFigure : function(){
		return this.rootFigure;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dGraphicalEditor.onPropertyChange<br>
	 * 函数功能：侦听模型事件信息<br>
	 * 参数输入：evt:PropertyChangeEvent<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    onPropertyChange : function(evt){
        var eventName = evt.getEventName();
        var oldValue  = evt.getOldValue();
        var newValue  = evt.getNewValue();
        if(eventName === Rainier.UI.V2.AbstractModel.MODEL_EVENT_ACTIVE_STATE_CHANGED){
            if(newValue.active === true){
                this.selected.add(newValue.model);
            }
            if(newValue.active === false){
            	this.selected.clear();
                //TODO:此段程序存在奇异，shift取消选择时，应该执行selected.remove(newValue.model)
            }
        }
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dGraphicalEditor.findPortModelById<br>
	 * 函数功能：根据父组件UUID和端口UUID搜索端口实例<br>
	 * 参数输入：parentId:String(uuid)<br>
	 * 参数输入：portId:String(uuid)<br>
	 * 函数返回：PortModel<br>
	 * 异常信息：无<br>
	 */
	findPortModelById : function(parentId,portId){
		var foundParent = null;
		var foundPort	= null;
		var rootModel 	= this.rootModel; 
		var children    = rootModel.getChildren();
		var connections = rootModel.getConnections();
		$.each(children,function(i,model){
			if(foundParent){
				return false;
			}
			if(model.getUuid() === parentId){
				foundParent = model;
			}
		});
		connections.each(function(i,conn){
			if(foundParent){
				return false;
			}
			if(conn.getChildren().length === 0){
				return;
			};
			var connChildren = conn.getChildren();
			$.each(connChildren,function(i,joint){
				if(foundParent){
					return false;
				}
				if(joint.getUuid() === parentId){
					foundParent = joint;
				}
			});
		});
		
		if(!foundParent){
			foundPort = null;
			return foundPort;
		}
		
		var inputs  = foundParent.getInputPorts();
		var outputs = foundParent.getOutputPorts();
		var hybrids = foundParent.getHybridPorts();
		
		inputs.each(function(i,port){
			if(foundPort){
				return false;
			}
			if(port.getUuid() === portId){
				foundPort = port;
			}
		});
		
		outputs.each(function(i,port){
			if(foundPort){
				return false;
			}
			if(port.getUuid() === portId){
				foundPort = port;
			}
		});
		
		hybrids.each(function(i,port){
			if(foundPort){
				return false;
			}
			if(port.getUuid() === portId){
				foundPort = port;
			}
		});
		
		return foundPort;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dGraphicalEditor.findEditPartById<br>
	 * 函数功能：通过ID号搜索EditPart<br>
	 * 参数输入：uuid:String<br>
	 * 函数返回：Rainier.UI.V2.AbstractEditPart<br>
	 * 异常信息：<br>
	 */
	findEditPartById : function(uuid){
		var found = null;
		var rootEditPart = this.rootEditPart;
    	Rainier.Basic.CompositeObject.recursiveEach(rootEditPart,function(part){
    		if(part instanceof Rainier.UI.V2.CompositeEditPart){
    			var inputparts 	= part.getInputParts();
    			var outputparts = part.getOutputParts();
    			var hybridparts = part.getHybridParts();
    			var connsparts  = part.getConnectionParts();
    			
    			if(part.getUuid() === uuid){
    				found = part;
		    		return false;
    			}
    			
    			inputparts.each(function(i,portpart){
    				if(portpart.getUuid() === uuid){
    					found = portpart;
		    			return false;
    				}
    			});
    			
    			outputparts.each(function(i,portpart){
    				if(portpart.getUuid() === uuid){
    					found = portpart;
		    			return false;
    				}
    			});
    			
    			hybridparts.each(function(i,portpart){
    				if(portpart.getUuid() === uuid){
    					found = portpart;
		    			return false;
    				}
    			});
    			
    			connsparts.each(function(i,connpart){
    				if(connpart.getUuid() === uuid){
    					found = connpart;
    	    			return false;
    				}
    				
    				return Rainier.Basic.CompositeObject.recursiveEach(connpart,function(_part){
    					if(found){
    		    			return false;
    					}
    	    			var __model = _part.getModel();
    	    			if(__model.getUuid() === uuid){
    	        			return false;
    	    			}
    	    			var _hybridparts = _part.getHybridParts();
    	    			_hybridparts.each(function(j,_portpart){
        					if(found){
        		    			return false;
        					}
    	    				if(_portpart.getUuid() === uuid){
    	    					found = _portpart;
    	    	    			return false;
    	    				}
    	    			});
    				});

    			});
        		if(found){
        			return false;
        		}
    		}
    	});
    	return found;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dGraphicalEditor.findModelById<br>
	 * 函数功能：通过ID号搜索Model<br>
	 * 参数输入：uuid:String<br>
	 * 函数返回：Rainier.UI.V2.AbstractModel<br>
	 * 异常信息：<br>
	 */
	findModelById : function(uuid){
		var found = null;
		var rootEditPart = this.rootEditPart;
    	Rainier.Basic.CompositeObject.recursiveEach(rootEditPart,function(part){
    		if(part instanceof Rainier.UI.V2.CompositeEditPart){
    			var _model 		= part.getModel();
    			var inputparts 	= part.getInputParts();
    			var outputparts = part.getOutputParts();
    			var hybridparts = part.getHybridParts();
    			var connsparts  = part.getConnectionParts();
    			
    			if(_model.getUuid() === uuid){
    				found = _model;
        			return false;
    			}
    			
    			inputparts.each(function(i,portpart){
    				var portModel = portpart.getModel();
    				if(portModel.getUuid() === uuid){
    					found = portModel;
    	    			return false;
    				}
    			});
    			
    			outputparts.each(function(i,portpart){
       				var portModel = portpart.getModel();
    				if(portModel.getUuid() === uuid){
    					found = portModel;
    	    			return false;
    				}
    			});
    			
    			hybridparts.each(function(i,portpart){
       				var portModel = portpart.getModel();
    				if(portModel.getUuid() === uuid){
    					found = portModel;
    	    			return false;
    				}
    			});
    			
    			connsparts.each(function(i,connpart){
       				var connModel = connpart.getModel();
    				if(connModel.getUuid() === uuid){
    					found = connModel;
		    			return false;
    				}
    				return Rainier.Basic.CompositeObject.recursiveEach(connpart,function(_part){
    	    			var __model 	 = _part.getModel();
    	    			var _hybridparts = _part.getHybridParts();
    	    			_hybridparts.each(function(j,_portpart){
    	       				var _portModel = _portpart.getModel();
    	    				if(_portModel.getUuid() === uuid){
    	    					found = _portModel;
    	    	    			return false;
    	    				}
    	    			});
    					if(found){
    		    			return false;
    					}
    				});
    			});
        		if(found){
        			return false;
        		}
    		}
    	});
    	return found;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dContext.findEditPartByModel<br>
	 * 函数功能：通过模型找到相应的EditPart<br>
	 * 参数输入：model<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	findEditPartByModel : function(model){
		var found = null;
		var rootEditPart = this.rootEditPart;
    	Rainier.Basic.CompositeObject.recursiveEach(rootEditPart,function(part){
    		if(part instanceof Rainier.UI.V2.CompositeEditPart){
    			var _model 		= part.getModel();
    			var inputparts 	= part.getInputParts();
    			var outputparts = part.getOutputParts();
    			var hybridparts = part.getHybridParts();
    			var connsparts  = part.getConnectionParts();
    			
    			if(_model === model){
    				found = part;
        			return false;
    			}
    			
    			inputparts.each(function(i,portpart){
    				var portModel = portpart.getModel();
    				if(portModel === model){
    					found = portpart;
            			return false;
    				}
    			});
    			
    			outputparts.each(function(i,portpart){
       				var portModel = portpart.getModel();
    				if(portModel === model){
    					found = portpart;
            			return false;
    				}
    			});
    			
    			hybridparts.each(function(i,portpart){
       				var portModel = portpart.getModel();
    				if(portModel === model){
    					found = portpart;
            			return false;
    				}
    			});
    			
    			connsparts.each(function(i,connpart){
       				var portModel = connpart.getModel();
    				if(portModel === model){
    					found = connpart;
            			return false;
    				}
    				
    				return Rainier.Basic.CompositeObject.recursiveEach(connpart,function(_part){
    	    			var __model 	 = _part.getModel();
    	    			var _hybridparts = _part.getHybridParts();
    	    			if(__model === model){
    	    				found = _part;
    	        			return false;
    	    			}
    	    			_hybridparts.each(function(j,_portpart){
    	    				if(_portpart.getModel() === model){
    	    					found = _portpart;
    	            			return false;
    	    				}
    	    			});
    					if(found){
    	        			return false;
    					}
    				});

    			});
        		if(found){
        			return false;
        		}
    		}
    	});
    	
    	return found;
 
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dContext.findEditPartByFigure<br>
	 * 函数功能：通过模型找到相应的EditPart<br>
	 * 参数输入：figure<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	findEditPartByFigure : function(figure){
		var found = null;
		var rootEditPart = this.rootEditPart;
    	Rainier.Basic.CompositeObject.recursiveEach(rootEditPart,function(part){
    		if(part instanceof Rainier.UI.V2.CompositeEditPart){
    			var _figure		= part.getFigure();
    			var inputparts 	= part.getInputParts();
    			var outputparts = part.getOutputParts();
    			var hybridparts = part.getHybridParts();
    			var connsparts  = part.getConnectionParts();
    			
    			if(_figure === figure){
    				found = part;
	    			return false;
    			}
    			
    			inputparts.each(function(i,portpart){
    				var portFigure = portpart.getFigure();
    				if(portFigure === figure){
    					found = portpart;
    	    			return false;
    				}
    			});
    			
    			outputparts.each(function(i,portpart){
    				var portFigure = portpart.getFigure();
    				if(portFigure === figure){
    					found = portpart;
    	    			return false;
    				}
    			});
    			
    			hybridparts.each(function(i,portpart){
    				var portFigure = portpart.getFigure();
    				if(portFigure === figure){
    					found = portpart;
    	    			return false;
    				}
    			});
    			
    			connsparts.each(function(i,connpart){
    				var connFigure = connpart.getFigure();
    				if(connFigure === figure){
    					found = connpart;
    	    			return false;
    				}
    				
    				return Rainier.Basic.CompositeObject.recursiveEach(connpart,function(_part){
    	    			var __figure 	 = _part.getFigure();
    	    			var _hybridparts = _part.getHybridParts();
    	    			if(__figure === figure){
    	    				found = _part;
        	    			return false;
    	    			}
    	    			_hybridparts.each(function(j,_portpart){
    	    				if(_portpart.getFigure() === figure){
    	    					found = _portpart;
    	    	    			return false;
    	    				}
    	    			});
    					if(found){
        	    			return false;
    					}
    				});

    			});
        		if(found){
        			return false;
        		}
    		}
    	});
    	
    	return found;

	},
	
	/**
	 * 函数名称：Rainier.UI.V2.Draw2dGraphicalEditor.findVirtualModelByLabel<br>
	 * 函数功能：通过label值搜索Model<br>
	 * 参数输入：uuid:String<br>
	 * 函数返回：Rainier.UI.V2.AbstractModel<br>
	 * 异常信息：<br>
	 */
	findVirtualModelByLabel : function(label){
		var found = null;
		var rootEditPart = this.rootEditPart;
    	Rainier.Basic.CompositeObject.recursiveEach(rootEditPart,function(part){
    		if(part instanceof Rainier.UI.V2.CompositeEditPart){
    			var _model   	  = part.getModel();
    			if(_model instanceof Rainier.UI.V2.RootModel){
    				return;
    			}
				var modelLabel  = _model.getProperty().modelProperty.label;
				if(modelLabel.toLowerCase() === label.toLowerCase()){
					found = _model;
					return false;
				}
    		}
    	});
    	return found;
	},
});
